//
//  BQShopProductCell.m
//  BeeQuick
//
//  Created by mac on 16/11/21.
//  Copyright © 2016年 six_group. All rights reserved.
//

#import "BQShopProductCell.h"
#import <UIImageView+WebCache.h>
#import "BQMyCarTool.h"
@interface BQShopProductCell()

//是否选中的按钮

@property (weak, nonatomic) IBOutlet UIButton *isSelectedBtn;


//商品图片
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
//商品名字
@property (weak, nonatomic) IBOutlet UILabel *productName;
//这类商品总价格
@property (weak, nonatomic) IBOutlet UILabel *totoalPrice;
//减号按钮
@property (weak, nonatomic) IBOutlet UIButton *reduceButton;
//加号按钮
@property (weak, nonatomic) IBOutlet UIButton *increaseButton;
//购买数量
@property (weak, nonatomic) IBOutlet UILabel *buyNum;

@end

@implementation BQShopProductCell


#pragma 选中按钮的点击
- (IBAction)isSelectBtnClick:(UIButton *)sender {
    
    
    _isSelectedBtn.selected = !_isSelectedBtn.selected;
    
    
    self.model.Selected = _isSelectedBtn.selected;
    
    //为自己的属性 取反
//    self.model.Selected = self.model.outSelected;
//    if(self.model.outSelected == YES)
////    {
////        [myCar deleteOneCategory:self.model];
//    }else
//    {
//        [myCar addOneCategory:self.model];
//    }
    
    
    //如果不选中的话
//    if(_isSelectedBtn.selected == NO)
//    {
#warning 这里不管选不选中, 都发通知,通告底部的那个栏更新一下
        //这里把按钮传过去
     [[NSNotificationCenter defaultCenter]postNotificationName:@"showTotalPrice" object:self.isSelectedBtn];
//    }
    
}



#pragma mark - 加号按钮的点击方法
- (IBAction)plusButtonClick:(UIButton *)sender {
    
    NSLog(@"我点击了加号");
    
    self.buyNum.hidden = NO;
    
    self.reduceButton.hidden = NO;
    
    [[BQMyCarTool shareMyCar]addGoods:_model];
    
    NSLog(@"%zd",_model.buyCount);
    self.buyNum.text = @(_model.buyCount).description;
    
    
   //发通知
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"showTotalPrice" object:nil];
    
    
}


#pragma mark - 减号按钮的点击方法
- (IBAction)reduceButtonClick:(UIButton *)sender {
    
    
    [[BQMyCarTool shareMyCar]removeGoods:_model];
    NSLog(@"我点击了减号");
    if(_model.buyCount == 0)
    {
        self.reduceButton.hidden = YES;
//        NSLog(@"%zd",_model.buyCount);
//        self.buyNum.text = @(_model.buyCount).description;
        self.buyNum.hidden = YES;
        
        
        if([self.delegate respondsToSelector:@selector(deleteGoodsWithCell:andModel:)])
        {
            [self.delegate deleteGoodsWithCell:self andModel:self.model];
        }
//        //MARK: - 在外面弹出一个提示框.
//        
//        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"删除商品" message:@"确认删除此商品吗?" preferredStyle:UIAlertControllerStyleAlert];
//        
//        // Create the actions.
//        
//        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//            NSLog(@"取消");
//        }];
//     
//        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//            NSLog(@"确认");
//        }];
//        
//        // Add the actions.
//        [alertVc addAction:cancelAction];
//        [alertVc addAction:otherAction];
        
        
        
        
       
    }
    else
    {
        
        self.buyNum.hidden = NO;
        self.reduceButton.hidden = NO;
        self.buyNum.text = @(_model.buyCount).description;
        NSLog(@"%zd",_model.buyCount);
        
    }
    
    
    //加减号都要使用通知
    
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"showTotalPrice" object:nil];
}



- (void)setModel:(BQHomeGoodModel *)model
{
    _model = model;
    
    
    self.productName.text = model.name;
    
    self.totoalPrice.text = model.price;
    
    self.isSelectedBtn.selected = model.Selected;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:model.img]];//加图片
    
    self.totoalPrice.text = [NSString stringWithFormat:@"¥%@",_model.price];
    
    self.buyNum.text = @(model.buyCount).description;
    
    if(model.buyCount <= 0)
    {
        self.reduceButton.hidden = YES;
        
//        NSLog(@"%zd",model.buyCount);
    }
    else
    {
        self.buyNum.hidden = NO;
        NSLog(@"%zd",model.buyCount);
        self.reduceButton.hidden = NO;
    }
    
    
    
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    //MARK: - 设置tableView的选中样式.
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //给个默认值 一开始就是选中的
    
        _isSelectedBtn.selected = YES;
    
    
    
//    //在根据model来判断
//    
//    if(self.model.outSelected == NO)
//    {
//        
//    }
//    else
//    {
//        _isSelectedBtn.selected = NO;
//    }
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
